﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;

namespace Com.PhilChuang.Utils.Mobile
{
	public class AppFileWp7Impl : IAppFile
	{
		public bool DirectoryExists (String path)
		{
			path.ThrowIfNull ("path");
			using (var storage = IsolatedStorageFile.GetUserStoreForApplication ())
			{
				return storage.DirectoryExists (path);
			}
		}

		public void CreateDirectory (String path)
		{
			path.ThrowIfNull ("path");
			using (var storage = IsolatedStorageFile.GetUserStoreForApplication ())
			{
				storage.CreateDirectory (path);
			}
		}

		public void CreateDirectoryIfNotExists (String path)
		{
			path.ThrowIfNull ("path");
			if (!DirectoryExists (path))
				CreateDirectory (path);
		}

		public IEnumerable<String> GetFilesIn (String path)
		{
			path.ThrowIfNull ("path");
			if (!DirectoryExists (path))
				throw new DirectoryNotFoundException (path);
			using (var storage = IsolatedStorageFile.GetUserStoreForApplication ())
			{
				foreach (var file in storage.GetFileNames (Path.Combine (path, "*")))
				{
					yield return Path.Combine (path, file);
				}
			}
			yield break;
		}

		public void FileDelete (String path)
		{
			path.ThrowIfNull ("path");
			using (var storage = IsolatedStorageFile.GetUserStoreForApplication ())
			{
				if (storage.FileExists (path))
					storage.DeleteFile (path);
			}
		}

		public bool FileExists (String path)
		{
			path.ThrowIfNull ("path");
			using (var storage = IsolatedStorageFile.GetUserStoreForApplication ())
			{
				return storage.FileExists (path);
			}
		}

		public void FileOpenRead (String path, Action<Stream> doStreamWork)
		{
			path.ThrowIfNull ("path");
			doStreamWork.ThrowIfNull ("doStreamWork");
			using (var storage = IsolatedStorageFile.GetUserStoreForApplication ())
			using (var stream = storage.OpenFile (path, FileMode.Open))
			{
				doStreamWork (stream);
			}
		}

		public void FileCreate (String path, Action<Stream> doStreamWork)
		{
			path.ThrowIfNull ("path");
			doStreamWork.ThrowIfNull ("doStreamWork");
			using (var storage = IsolatedStorageFile.GetUserStoreForApplication ())
			{
				var dir = Path.GetDirectoryName (path);
				if (!storage.DirectoryExists (dir))
					storage.CreateDirectory (dir);
				using (var stream = storage.OpenFile (path, FileMode.Create))
				{
					doStreamWork (stream);
				}
			}
		}

		public void FileAppendText (String path, Action<StreamWriter> doStreamWriterWork)
		{
			path.ThrowIfNull ("path");
			doStreamWriterWork.ThrowIfNull ("doStreamWriterWork");
			using (var storage = IsolatedStorageFile.GetUserStoreForApplication ())
			using (var stream = storage.OpenFile (path, FileMode.Append))
			using (var sw = new StreamWriter (stream))
			{
				doStreamWriterWork (sw);
			}
		}

		public long GetFreeSpace ()
		{
			using (var storage = IsolatedStorageFile.GetUserStoreForApplication ())
			{
				return storage.AvailableFreeSpace;
			}
		}
	}
}
